﻿using Areaexportimg.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Areaexportimg.Utils
{
    /// <summary>
    /// use ConnectionFactory.Config to config,database connectionn factory,now only support mysql
    /// </summary>
    public class ConnectionFactory
    {
        public static void Config(DatabaseType type, string ConnectionStrings)
        {
            strConn = ConnectionStrings;
            dbType = type;
        }
        private static string strConn = string.Empty;
        private static DatabaseType dbType;
        /// <summary>
        /// 转换数据库类型
        /// </summary>
        /// <param name="databaseType">数据库类型</param>
        /// <returns></returns>
        private DatabaseType GetDataBaseType(string databaseType)
        {
            DatabaseType returnValue = DatabaseType.SqlServer;
            foreach (DatabaseType dbType in Enum.GetValues(typeof(DatabaseType)))
            {
                if (dbType.ToString().Equals(databaseType, StringComparison.OrdinalIgnoreCase))
                {
                    returnValue = dbType;
                    break;
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateConnection()
        {
            IDbConnection connection = null;

            switch (dbType)
            {
                case DatabaseType.SqlServer:
                    //connection = new System.Data.SqlClient.SqlConnection(strConn);
                    break;
                case DatabaseType.Mysql:
                    connection = new MySql.Data.MySqlClient.MySqlConnection(strConn);
                    break;
                case DatabaseType.Sqlite:
                    // connection = new SQLiteConnection(strConn);
                    break;
                case DatabaseType.Oracle:
                    //connection = new Oracle.ManagedDataAccess.Client.OracleConnection(strConn);
                    //connection = new System.Data.OracleClient.OracleConnection(strConn);
                    break;
            }

            return connection;
        }
    }

}
